/*
    This file is part of the substandard lisp implementation of Standard Lisp
    Copyright (C) 2011 Nicky Nickell

    substandard lisp is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/*
    builtins MAP composites as defined in section 3.12 of the
    The Standard Lisp Report
*/

function std_map(a, env) {
  var x = a[0];
  var fn = a[1];

  for (var p = x; p != NIL; p = p.cdr) {
    evaluate(APPLY1(fn, QUOTE(p)), env);
  }

  return NIL;
}
builtins['map'] = new builtin_function(std_map, 2, false, [TYPE_PAIR, TYPE_FUNCTION], FTYPE_EXPR);

function std_mapc(a, env) {
  var x = a[0];
  var fn = a[1];

  for (var p = x; p != NIL; p = p.cdr) {
    evaluate(APPLY1(fn, QUOTE(p.car)), env);
  }

  return NIL;
}
builtins['mapc'] = new builtin_function(std_mapc, 2, false, [TYPE_PAIR, TYPE_FUNCTION], FTYPE_EXPR);

interpreted_builtins['mapcan'] = 
"(de mapcan (x fn) \
  (cond \
    ((null x) NIL) \
    (T (nconc (fn (car x)) (mapcan (cdr x) fn)))))";

interpreted_builtins['mapcar'] = 
"(de mapcar (x fn) \
  (cond \
    ((null x) NIL) \
    (T (cons (fn (car x)) (mapcar (cdr x) fn)))))";

interpreted_builtins['mapcon'] = 
"(de mapcon (x fn) \
  (cond \
    ((null x) NIL) \
    (T (nconc (fn x) (mapcon (cdr x) fn)))))";

interpreted_builtins['maplist'] = 
"(de maplist (x fn) \
  (cond \
    ((null x) NIL) \
    (T (cons (fn x) (maplist (cdr x) fn)))))";
